package org.example.eatback.mappers;


import org.apache.ibatis.annotations.*;
import org.example.eatback.pojo.DiaryRecord;

import java.time.LocalDate;

@Mapper
public interface DiaryMapper {

    @Select("SELECT * FROM diary_record WHERE username = #{username} AND record_date = #{date}")
    DiaryRecord findByUsernameAndDate(@Param("username") String username, @Param("date") LocalDate date);

    @Insert("INSERT INTO diary_record (username, record_date, water, calories, weight, " +
            "breakfast_foods, breakfast_calories, breakfast_notes, " +
            "lunch_foods, lunch_calories, lunch_notes, " +
            "dinner_foods, dinner_calories, dinner_notes) " +
            "VALUES (#{username}, #{recordDate}, #{water}, #{calories}, #{weight}, " +
            "#{breakfastFoods}, #{breakfastCalories}, #{breakfastNotes}, " +
            "#{lunchFoods}, #{lunchCalories}, #{lunchNotes}, " +
            "#{dinnerFoods}, #{dinnerCalories}, #{dinnerNotes})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(DiaryRecord record);

    @Update("UPDATE diary_record SET water = #{water}, calories = #{calories}, weight = #{weight}, " +
            "breakfast_foods = #{breakfastFoods}, breakfast_calories = #{breakfastCalories}, breakfast_notes = #{breakfastNotes}, " +
            "lunch_foods = #{lunchFoods}, lunch_calories = #{lunchCalories}, lunch_notes = #{lunchNotes}, " +
            "dinner_foods = #{dinnerFoods}, dinner_calories = #{dinnerCalories}, dinner_notes = #{dinnerNotes} " +
            "WHERE username = #{username} AND record_date = #{recordDate}")
    void update(DiaryRecord record);

    @Update("UPDATE diary_record SET " +
            "${mealType}_foods = #{foods}, " +
            "${mealType}_calories = #{calories}, " +
            "${mealType}_notes = #{notes} " +
            "WHERE username = #{username} AND record_date = #{date}")
    void updateMeal(@Param("username") String username,
                    @Param("date") LocalDate date,
                    @Param("mealType") String mealType,
                    @Param("foods") String foods,
                    @Param("calories") Integer calories,
                    @Param("notes") String notes);

    @Update("UPDATE diary_record SET " +
            "${mealType}_foods = NULL, " +
            "${mealType}_calories = 0, " +
            "${mealType}_notes = NULL " +
            "WHERE username = #{username} AND record_date = #{date}")
    void deleteMeal(@Param("username") String username,
                    @Param("date") LocalDate date,
                    @Param("mealType") String mealType);


    @Select("SELECT COUNT(*) FROM diary_record")
    int count();
}
